<?php

namespace App\Swoole\Server;

use App\Jobs\SocketSendSms;
use App\Jobs\SocketSendSmsExt;
use \Throwable;
use SwooleTW\Http\Server\Sandbox;
use SwooleTW\Http\Transformers\Request;
use SwooleTW\Http\Transformers\Response;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Laravel\Lumen\Http\Request as LumenRequest;
use Swoole\Coroutine;
use GuzzleHttp\DefaultHandler;
use Yurun\Util\Swoole\Guzzle\SwooleHandler;

/**
 * Class Manager
 */
class Manager extends \SwooleTW\Http\Server\Manager
{


    /**
     * "onRequest" listener.
     *
     * @param \Swoole\Http\Request $swooleRequest
     * @param \Swoole\Http\Response $swooleResponse
     */
    public function onRequest($swooleRequest, $swooleResponse)
    {
        $this->app->make('events')->dispatch('swoole.request');

        $this->resetOnRequest();
        $sandbox = $this->app->make(Sandbox::class);
        $handleStatic = $this->container->make('config')->get('swoole_http.server.handle_static_files', true);
        $publicPath = $this->container->make('config')->get('swoole_http.server.public_path', base_path('public'));

        try {
            // handle static file request first
            if ($handleStatic && Request::handleStatic($swooleRequest, $swooleResponse, $publicPath)) {
                return;
            }
            // transform swoole request to illuminate request
            $illuminateRequest = Request::make($swooleRequest)->toIlluminate();

            if (!$sandbox->isLaravel()) { // is lumen app
                $illuminateRequest = LumenRequest::createFromBase($illuminateRequest);
            }
            $illuminateRequest->swooleResponse = $swooleResponse;
            // set current request to sandbox
            $sandbox->setRequest($illuminateRequest);

            // enable sandbox
            $sandbox->enable();

            // handle request via laravel/lumen's dispatcher
            //$illuminateResponse = null;
            //Coroutine::create(function () use(&$illuminateResponse,$sandbox,$illuminateRequest){
                $illuminateResponse = $sandbox->run($illuminateRequest);
            //});

            // send response
            Response::make($illuminateResponse, $swooleResponse, $swooleRequest)->send();
        } catch (Throwable $e) {
            try {
                $exceptionResponse = $this->app
                    ->make(ExceptionHandler::class)
                    ->render(
                        $illuminateRequest,
                        $this->normalizeException($e)
                    );
                Response::make($exceptionResponse, $swooleResponse, $swooleRequest)->send();
            } catch (Throwable $e) {
                $this->logServerError($e);
            }
        } finally {
            // disable and recycle sandbox resource
            $sandbox->disable();
        }
    }

    public function run()
    {
        if(class_exists('GuzzleHttp\\DefaultHandler')){
            //DefaultHandler::setDefaultHandler(SwooleHandler::class);
        }
        parent::run(); // TODO: Change the autogenerated stub
    }


}
